package net.sxnic.ugr.role.action;

import net.sxinfo.core.dao.Page;
import net.sxinfo.core.dao.hibernate3.HibernateCriteria;
import net.sxinfo.core.dao.hibernate3.HibernateDaoUtils;
import net.sxinfo.core.dao.hibernate3.HibernateOrder;
import net.sxinfo.core.util.WebUtils;
import net.sxnic.comm.utils.PropertyUtil;
import net.sxnic.ugr.UgrActionSupport;
import net.sxnic.ugr.role.Role;
import net.sxnic.ugr.role.RoleManager;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 
 * Introduction：
 * 
 * @author 孙宇飞 create date : 2010-5-16
 */
@SuppressWarnings("serial")
public class Browse extends UgrActionSupport {

	private Page roles;
	private String roleCode;
	private String roleName;

	@Autowired
	protected RoleManager roleManager;

	public String execute() throws Exception {

		try {
			checkRoles(Role.SYSTEM_ADMIN);

			int pageNumber = WebUtils.getPageNumber(request);
			int pageSize = PropertyUtil.findTableSize("firstbrowse.tabsize");

			HibernateOrder infoCodeOrder = HibernateDaoUtils
					.createHibernateOrder("role", true);
			HibernateCriteria hc = new HibernateCriteria().add(infoCodeOrder);

			Criterion dc = null;
			// 组合条件
			if (StringUtils.isNotBlank(roleCode)) {// 登录名
				dc = Restrictions.like("role", "%" + roleCode + "%");
				hc.add(dc);
			}
			if (StringUtils.isNotBlank(roleName)) {// 用户名
				dc = Restrictions.like("name", "%" + roleName + "%");
				hc.add(dc);
			}
			roles = roleManager.getPageByCriteria(pageNumber, pageSize, hc);

		} catch (Exception e) {
			e.printStackTrace();
		}

		return SUCCESS;
	}

	public Page getRoles() {
		return roles;
	}

	public void setRoles(Page roles) {
		this.roles = roles;
	}

	public String getRoleCode() {
		return roleCode;
	}

	public void setRoleCode(String roleCode) {
		this.roleCode = roleCode;
	}

	public String getRoleName() {
		return roleName;
	}

	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}

}
